<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>分析 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_analytics.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_analytics.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_analytics.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_analytics.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="intro.html">你知道的, 为了搜索…​</a></span>
»
<span class="breadcrumb-node">分析</span>
</div>
<div class="navheader">
<span class="prev">
<a href="highlighting-intro.html">« 高亮搜索</a>
</span>
<span class="next">
<a href="_tutorial_conclusion.html">教程结语 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_analytics"></a>分析<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/010_Intro/35_Tutorial_Aggregations.asciidoc">edit</a>
</h2>
</div></div></div>
<p>终于到了最后一个业务需求：允许管理者对员工目录做分析。 Elasticsearch 有一个功能叫 <em>聚合 (aggregations)</em>，允许我们基于数据生成一些精细的分析结果。它与 SQL 中的 <code class="literal">GROUP BY</code> 类似但更强大。</p>

<p>举个例子，找出员工中最受欢迎的兴趣爱好：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/010_Intro/35_Aggregations.json"></div>
<p>暂时忽略掉语法，直接看结果：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
   ...
   "hits": { ... },
   "aggregations": {
      "all_interests": {
         "buckets": [
            {
               "key":       "music",
               "doc_count": 2
            },
            {
               "key":       "forestry",
               "doc_count": 1
            },
            {
               "key":       "sports",
               "doc_count": 1
            }
         ]
      }
   }
}</pre>
</div>
<p>可以看到，两位员工对音乐感兴趣，一位对林业感兴趣，一位对运动感兴趣。

这些聚合数据并非预先统计的，而是根据当前查询匹配的文档实时生成的。

如果想知道叫 Smith 的员工中最受欢迎的兴趣爱好，只需向混合查询中添加合适的查询：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /megacorp/employee/_search
{
  "query": {
    "match": {
      "last_name": "smith"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/010_Intro/35_Aggregations.json"></div>
<p><code class="word">all_interests</code> 聚合已经变为只包含匹配查询的文档：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">  ...
  "all_interests": {
     "buckets": [
        {
           "key": "music",
           "doc_count": 2
        },
        {
           "key": "sports",
           "doc_count": 1
        }
     ]
  }</pre>
</div>
<p>聚合还支持分级汇总。比如，查询特定兴趣爱好员工的平均年龄：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /megacorp/employee/_search
{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/010_Intro/35_Aggregations.json"></div>
<p>得到的聚合结果有点儿复杂，但理解起来还是很简单的：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">  ...
  "all_interests": {
     "buckets": [
        {
           "key": "music",
           "doc_count": 2,
           "avg_age": {
              "value": 28.5
           }
        },
        {
           "key": "forestry",
           "doc_count": 1,
           "avg_age": {
              "value": 35
           }
        },
        {
           "key": "sports",
           "doc_count": 1,
           "avg_age": {
              "value": 25
           }
        }
     ]
  }</pre>
</div>
<p>输出基本是第一次聚合的加强版。依然有一个兴趣及数量的列表，只不过每个兴趣都有了一个附加的 <code class="word">avg_age</code>，代表有这个兴趣爱好的所有员工的平均年龄。</p>
<p>
即使你现在还不理解这些语法，但还是可以很容易的看到如何使用这个特性来实现复杂的聚合和分组。

能够提取的数据类型也没有任何限制。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="highlighting-intro.html">« 高亮搜索</a>
</span>
<span class="next">
<a href="_tutorial_conclusion.html">教程结语 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>